#pragma once
#include "DXAMLCommon.h"
#include "IXamlSchemaContext.h"
#include "XamlTypeToken.h"
#include "XamlPropertyToken.h"
#include "XamlAssemblyToken.h"
#include "XamlTypeNamespaceToken.h"


class XamlXmlNamespace;
class XamlNativeTypeInfoProvider;
class XamlManagedTypeInfoProvider;
class XamlNativeRuntime;
class XamlManagedRuntime;
class XamlType;
class XamlTextSyntax;
class XamlProperty;

class XamlAssembly;
class XamlTypeNamespace;
class XamlNamespace;
class CCoreServices;
class ParserErrorReporter;
class XamlRuntime;
class XamlTypeInfoProvider;
struct XamlQualifiedObject;

class XamlSchemaContext;
class XamlNamespace;


#include "XamlTypeNamespace.h"
#include "XamlType.h"
#include "XamlAssembly.h"


class XamlSchemaContext :
	public IXamlSchemaContext,
	public xenable_shared_from_this<XamlSchemaContext>
{
private: 
	XamlSchemaContext();
public: 
	static HRESULT          Create(CCoreServices * pCore, XBOOL bAllowXmlnsOnlyReset, const xshared_ptr<ParserErrorReporter> & inErrorReporter, xshared_ptr<XamlSchemaContext> & outXamlSchemaContext);
	static HRESULT          Create(CCoreServices * pCore, XBOOL bAllowXmlnsOnlyReset, xshared_ptr<XamlSchemaContext> & outXamlSchemaContext);
	virtual HRESULT         ResetXmlnsDefinitions();
	virtual HRESULT         AddAssembly(struct XamlAssemblyToken inAssemblyToken, const CXString * pstrAssemblyName);
	virtual HRESULT         AddAssemblyXmlnsDefinition(struct XamlAssemblyToken inAssembly, const CXString * pstrXmlnsUri, struct XamlTypeNamespaceToken inTypeNamespaceToken, const CXString * pstrTypeNamespace);
	virtual HRESULT         ResolvePropertyName(struct XamlTypeToken tokType, const CXString & pstrPropertyName, struct XamlPropertyToken & rtokProperty, struct XamlTypeToken & rtokPropertyType);
	virtual HRESULT         GetDeclaringType(struct XamlPropertyToken tokProperty, struct XamlTypeToken & tokDeclaringType);
	virtual XBOOL           DoesAllowXmlnsOnlyReset();
	HRESULT                 GetSourceAssembly(xshared_ptr<CXString> & outssSourceAssembly);
	HRESULT                 PushSourceAssembly(const xshared_ptr<CXString> & inssSourceAssembly);
	HRESULT                 PopSourceAssembly();
	HRESULT                 GetXamlNamespace(const xshared_ptr<CXString> & inXmlns, const xshared_ptr<XamlAssembly> & outAssembly, xshared_ptr<XamlNamespace> & outNamespace);
	HRESULT                 GetXamlNamespace(const xshared_ptr<CXString> & inXmlns, xshared_ptr<XamlNamespace> & outNamespace);

	HRESULT                 GetXamlNamespaceFromRuntimeIndex(XUINT32 uiIndex, xshared_ptr<XamlNamespace> & spXamlNamespace);
	HRESULT                 get_StringXamlType(xshared_ptr<XamlType> & spStringType);
	HRESULT                 get_Int32XamlType(xshared_ptr<XamlType> & spInt32Type);
	HRESULT                 get_StringSyntax(xshared_ptr<XamlTextSyntax> & outStringSyntax);
	HRESULT                 get_Int32Syntax(xshared_ptr<XamlTextSyntax> & outInt32Syntax);
	HRESULT                 get_PositionalParametersProperty(xshared_ptr<XamlProperty> &);
	HRESULT                 get_InitializationProperty(xshared_ptr<XamlProperty> & outInitializationProperty);
	HRESULT                 GetXamlDirective(const xshared_ptr<XamlNamespace> & inXamlNamespace, const xshared_ptr<CXString> inssName, xshared_ptr<XamlProperty> & outXamlProperty);
	HRESULT                 ItemsProperty(const xshared_ptr<XamlType> & inCollectionType, xshared_ptr<XamlProperty> & outItemsProperty);
	HRESULT                 get_SupportMarkupExtensionsWithDuplicateArity(XBOOL *);
	HRESULT                 GetXamlAssembly(const struct XamlAssemblyToken & inAssemblyToken, xshared_ptr<XamlAssembly> & outXamlAssembly);
	HRESULT                 GetXamlAssembly(const struct XamlAssemblyToken & inAssemblyToken, const xshared_ptr<CXString> & inAssemblyName, xshared_ptr<XamlAssembly> & outXamlAssembly);
	HRESULT                 GetXamlTextSyntax(const struct XamlTypeToken & sTypeToken, xshared_ptr<XamlTextSyntax> & outXamlTextSyntax);

	HRESULT                 GetXamlType(const struct XamlTypeToken & inTypeToken, const xshared_ptr<XamlNamespace> & inNamespace, xshared_ptr<XamlType> & outXamlType);
	HRESULT                 GetXamlType(const struct XamlTypeToken & inTypeToken, const xshared_ptr<XamlNamespace> & inNamespace, const xshared_ptr<CXString> inTypeName, xshared_ptr<XamlType> & outXamlType);
	HRESULT                 GetXamlType(const struct XamlTypeToken & inTypeToken, xshared_ptr<XamlType> & outXamlType);

	HRESULT                 GetXamlTypeFromRuntimeIndex(XUINT32 uiIndex, xshared_ptr<XamlType> & spXamlType);
	HRESULT                 CheckPeerType(const xshared_ptr<XamlQualifiedObject> & inQO, const xshared_ptr<CXString> & inssClassName, XBOOL bIsExactMatchRequired);

	HRESULT                 GetXamlProperty(const xshared_ptr<CXString> & spFullyQualifiedPropertyName, xshared_ptr<XamlProperty> & outXamlProperty);
	HRESULT                 GetXamlProperty(const struct XamlPropertyToken inPropertyToken, const struct XamlTypeToken inPropertyTypeToken, xshared_ptr<XamlProperty> & outXamlProperty);
	HRESULT                 GetXamlProperty(const struct XamlPropertyToken inPropertyToken, const struct XamlTypeToken inPropertyTypeToken, const xshared_ptr<CXString> & inPropertyName, xshared_ptr<XamlProperty> & outXamlProperty);
	HRESULT                 GetXamlProperty(const struct XamlPropertyToken inPropertyToken, xshared_ptr<XamlProperty> & outXamlProperty);

	HRESULT                 RegisterXamlProperty(const xshared_ptr<XamlProperty> & spXamlProperty);
	HRESULT                 GetXamlPropertyFromRuntimeIndex(unsigned int uiIndex, xshared_ptr<XamlProperty> & spXamlProperty);
	HRESULT                 GetXamlTypeNamespace(const struct XamlTypeNamespaceToken inTypeNamespaceToken, const xshared_ptr<CXString> & inTypeNamespaceName, const xshared_ptr<XamlAssembly> & inXamlAssembly, xshared_ptr<XamlTypeNamespace> & outXamlTypeNamespace);
	virtual HRESULT         GetTypeInfoProvider(enum XamlTypeInfoProviderKind tpk, xshared_ptr<XamlTypeInfoProvider> & outTypeInfoProvider);
	virtual HRESULT         GetRuntime(enum XamlFactoryKind fk, xshared_ptr<XamlRuntime> & rspXamlRuntime);
	HRESULT                 GetCore(CCoreServices * * ppCore);
	HRESULT                 GetErrorService(xshared_ptr<ParserErrorReporter> & outErrorService);
	HRESULT                 SetErrorService(const xshared_ptr<ParserErrorReporter> & inErrorService);
	HRESULT                 SetFoundIgnoredUsingXmlns();
	HRESULT                 OnAssemblyAdded();
	HRESULT                 CrackXmlns(XUINT32 cXmlns, const XCHAR * pXmlns, XCHAR * * ppClrNamespace, XCHAR * * ppAssembly);
private: 
	HRESULT                 EnsureAllXmlnsMappingsLoaded();
	HRESULT                 GetXmlnsMappings(xshared_ptr<XamlAssembly> &);
	HRESULT                 NormalizeUsingXmlNs(const CXString & rstrXmlNs, xshared_ptr<CXString> & outNormalizedXmlNs);
	static HRESULT          ExtractTypeNamespace(CCoreServices * pCore, const CXString & rstrXmlNs, xshared_ptr<CXString> & outTypeNamespace);
	HRESULT                 InitializeXmlNamespaceMap(XBOOL bPreserveExistingSpecialNamespaces);
	HRESULT                 FlushXamlNodeStreamCacheManager();
	HRESULT                 GetFullyQualifiedNameParts(const xshared_ptr<CXString> & spFullyQualifiedName, xshared_ptr<CXString> & outNamespaceUri, xshared_ptr<CXString> & outTypeName, xshared_ptr<CXString> & outPropertyName);
	HRESULT                 Initialize();

private: 
	xshared_ptr<XamlXmlNamespace> m_spXaml2006Ns; 
	xshared_ptr<XamlNativeTypeInfoProvider> m_spNativeTypeInfoProvider;  
	xshared_ptr<XamlManagedTypeInfoProvider> m_spManagedTypeInfoProvider; 
	xshared_ptr<XamlNativeRuntime> m_spNativeRuntime; 
	xshared_ptr<XamlManagedRuntime> m_spManagedRuntime;
	xshared_ptr<XamlType> m_spStringType; 
	xshared_ptr<XamlType> m_spInt32Type; 
	xshared_ptr<XamlTextSyntax> m_spStringSyntax; 
	xshared_ptr<XamlTextSyntax> m_spInt32Syntax; 
	xshared_ptr<XamlProperty> m_spPositionalParametersProperty; 
	xshared_ptr<XamlProperty> m_spInitializationProperty; 
	xshared_ptr<XamlProperty> m_spItemsProperty; 
	xstack<xshared_ptr<CXString> > m_SourceAssemblies; 

	xstringmap<xshared_ptr<XamlXmlNamespace> > m_mapUriToXmlNamespace; 
	xstringmap<xshared_ptr<XamlProperty> > m_mapFullyQualifiedProperty;  
	xchainedmap<XamlTypeToken,xshared_ptr<XamlType> > m_mapMasterTypeList; 
	xchainedmap<XamlPropertyToken,xshared_ptr<XamlProperty> > m_mapMasterMemberList; 
	xchainedmap<XamlAssemblyToken,xshared_ptr<XamlAssembly> > m_mapMasterAssemblyList;  
	xchainedmap<XamlTypeNamespaceToken,xshared_ptr<XamlTypeNamespace> > m_mapMasterTypeNamespaceList;  

	xvector<xshared_ptr<XamlType> > m_typeVector; 
	xvector<xshared_ptr<XamlProperty> > m_memberVector; 
	xvector<xshared_ptr<XamlNamespace> > m_xmlNamespaceVector;  
	CCoreServices * m_pCore;
	xshared_ptr<ParserErrorReporter> m_ErrorService; 
	XBOOL m_bAllowXmlnsOnlyReset;

};

